#!/bin/bash

# library of variables and functions for pim_gather/notify/...


#    (c) 2011 Donders Centre/Simon Oosthoek
#
#    This file is part of PIM.
#
#    PIM is free software: you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation, either version 2 of the License, or
#    (at your option) any later version.
#
#    PIM is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with PIM.  If not, see <http://www.gnu.org/licenses/>.
#
PROG=$(basename $0)
MACHINE=$(uname -n | sed 's/\..*//')
MEMSIZE=$(free | awk 'NR==2 {print $2}') #memsize in kB
EPOCHNOW=$(date +"%s")
SECPERDAY=$((3600*24))

SENDMAIL=/usr/sbin/sendmail
if [ ! -x $SENDMAIL ]
then
	echo "$PROG: sendmail executable $SENDMAIL not available" >&2
	exit 1
fi
 
pim_basedir=/home/common/Services/pim
pim_configdir=$pim_basedir/config
pim_datadir=$pim_basedir/data
pim_olddatadir=$pim_basedir/olddata
pim_useract=$pim_basedir/pidaction
pim_configprefix=pim_proc_
pim_conffile=$pim_configdir/pim_config
pim_vacation=$pim_configdir/pim_vacation

# sleep for a random time but no more than $1 seconds
function randomsleep {
	maxtime=$1
	sleeptime=$(($RANDOM % $maxtime))
	sleep $sleeptime
}


#convert the epoch value (seconds since 1 jan 1970)
# to a date and time value
function epoch2date {
	if [ $(($1+0)) -gt 0 ]
	then
		echo $(echo $1 | awk '{print strftime("%F %R", $0)}')
	else
		echo "no date"
	fi
}

#convert elapsed time notation to equivalent in seconds
function etime2sec {

	etime="$1"
	echo $(echo "$etime"|awk  -F'[-:]' '
		NF == 2 {
			# mm:ss
			print $1*60 + $2
		}
		NF == 3 {
			# hh:mm:ss
			print $1*3600 + $2*60 + $3
		}
		NF == 4 {
			# dd-hh:mm:ss
			print $1*3600*24 + $2*3600 + $3*60 + $4
		} ')
}



#convert the elapsed time notation to just days
function etime2day {
	etime="$1"
	echo $(echo "$etime"|awk  -F'[-:]' '
		NF < 4 {
			# mm:ss or hh:mm:ss
			print "0"
		}
		NF == 4 {
			# dd-hh:mm:ss
			print $1
		} ')
}

# renew user process
function renew_pid {
	r_pidfile=$1
	r_renewed=$2

	if [ -w $r_pidfile ]
	then
		echo "pid_renewed=\"$r_renewed\"" >>$r_pidfile
		echo "pid_renewed_human=\"$(epoch2date $r_renewed)\"" >>$r_pidfile
	fi
}

# renew all monitored user processes associated with a user
# on a particular machine
function renew_session {
	r_user=$1
	r_mach=$2
	r_renewed=$3

	for f in $pim_datadir/${r_user}_${r_mach}_*
	do
		r_pidfile=$pim_datadir/$(basename $f | sed 's/^[a-z]*_//')
		renew_pid $r_pidfile $r_renewed
	done
}

# test whether a user is currently in the vacation file
function exclude_user {
	user=$1
	if [ -r $pim_vacation ]
	then
		if grep -- "^$user" $pim_vacation >/dev/null 2>&1
		then
			return 0
		else
			return 1
		fi
	else
		return 1
	fi
}
		
function quiet_rm {
	if [ $# -ne 1 ]
	then
		return 1
	fi
	fname="$1"
	if [ -f "$fname" ]
	then
		rm -f "$fname"
	elif [ -d "$fname" ]
	then
		rm -rf "$fname" 
	fi
}

function quiet_mv {
	if [ $# -ne 2 ]
	then
		return 1
	fi
	src="$1"
	dst="$2"
	if [ -e "$src" ] 
	then
		mv "$src" "$dst"
	fi
}
	
